<template>
  <div>
    <el-form :inline="true" :model="cform" class="demo-form-inline">
      <el-form-item label="一级分类">
        <el-select placeholder="请选择" v-model="cform.category1Id" @change="handler1" :disabled="show">
          <el-option :label="c1.name" :value="c1.id" v-for="(c1, index) in list1" :key="c1.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select placeholder="请选择" v-model="cform.category2Id" @change="handler2" :disabled="show">
          <el-option :label="c2.name" :value="c2.id" v-for="(c2, index) in list2" :key="c2.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select placeholder="请选择" v-model="cform.category3Id" @change="handler3" :disabled="show">
          <el-option :label="c3.name" :value="c3.id" v-for="(c3, index) in list3" :key="c3.id"></el-option>
        </el-select>
      </el-form-item>

    </el-form>
  </div>
</template>

<script>
export default {
  name: "CategorySelect",
  props: ['show'],

  data() {
    return {
        list1: [],
        list2: [],
        list3: [],
        cform: {
            category1Id: '',
            category2Id: '',
            category3Id: ''
        }
    };
  },

  mounted() {
    this.getCategory1List()
  },

  methods: {
    async getCategory1List(){
        let result = await this.$API.attr.reqCategory1List()
        if(result.code == 200){
            this.list1 = result.data
        }
    },
    async handler1(){
        //先重置category2Id、category3Id、list2、list3
        this.cform.category2Id = ''
        this.cform.category3Id = ''
        this.list2 = []
        this.list3 = []

        const { category1Id, category2Id, category3Id } = this.cform
        this.$emit('getInfoList', { category1Id, category2Id, category3Id })
        let result = await this.$API.attr.reqCategory2List(category1Id)
        if(result.code == 200){
            this.list2 = result.data
        }
    },
    async handler2(){
        //先重置category3Id、list3
        this.cform.category3Id = ''
        this.list3 = []

        const { category1Id, category2Id, category3Id } = this.cform
        this.$emit('getInfoList', { category1Id, category2Id, category3Id })
        let result = await this.$API.attr.reqCategory3List(category2Id)
        if(result.code == 200){
            this.list3 = result.data
        }
    },
    handler3(){
        const { category1Id, category2Id, category3Id } = this.cform
        this.$emit('getInfoList', { category1Id, category2Id, category3Id })
    }
  },
};
</script>

<style scoped></style>
